package com.itheima.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.dao.OrderDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.MD5Utils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class,weight = 100)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    //根据月份统计会员数量
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for(String m : month){
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }
    //更改密码
    public void updatePassword(String newpassword,String telephone) {
        memberDao.updatePassword(newpassword,telephone);
    }

    public void update(Member member) {
        memberDao.update(member);
    }

    @Override
    public Member findById(String id) {
        return memberDao.findById(Integer.parseInt(id));
    }


    @Override
    public void backgroundAdd(Member member, Integer healthManagerId) {
        memberDao.add(member);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        for (Member member : page) {
            Date birthday = member.getBirthday();
            if (birthday != null) {
                Calendar cal = Calendar.getInstance();
                Calendar bir = Calendar.getInstance();
                bir.setTime(birthday);
                int year = cal.get(Calendar.YEAR) - bir.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH) - bir.get(Calendar.MONTH);
                if (month < 0) {
                    year--;
                }
                member.setAge(year);
            }


        }
        long total = page.getTotal();
        List<Member> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void delete(Integer id) {
        memberDao.deleteById(id);
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }

    @Autowired
    private SetmealDao setmealDao;
    /**
     * 查询所有信息用于导出表
     * @param memberIds
     * @return
     */
    @Override
    public List<Map> exportMemberReport(Integer[] memberIds) {
        List<Member> members = memberDao.exportMemberReport(memberIds);
        List<Map> mapList = new ArrayList<>();
        for (Member member : members) {
            HashMap map = new HashMap<>();
            List<Integer> list = setmealDao.findSetmealById(member.getId());
            String fileNumber = member.getFileNumber();
            String name = member.getName();
            String sex = member.getSex();
            sex = sex ==null ? sex : sex.equals("1") ? "男" : "女" ;
            String phoneNumber = member.getPhoneNumber();
            map.put("fileNumber", fileNumber);
            map.put("name",name);
            map.put("sex",sex);
            map.put("phoneNumber",phoneNumber);
            mapList.add(map);
            List<String> setmealNames = new ArrayList<>();
            if (list != null && list.size() >0){
                for (Integer setmealId : list) {
                    //一个id对应的套餐信息
                    Setmeal setmeal = setmealDao.findById(setmealId);
                    List<String> checkGroupNames = new ArrayList<>();
                    List<CheckGroup> checkGroups = setmeal.getCheckGroups();
                    for (CheckGroup checkGroup : checkGroups) {
                        List<CheckItem> checkItems = checkGroup.getCheckItems();
                        List<String> checkItemNames = new ArrayList<>();
                        for (CheckItem checkItem : checkItems) {
                            checkItemNames.add(checkItem.getName());
                        }
                        map.put("checkItemNames",checkItemNames.toString());
                        checkGroupNames.add(checkGroup.getName());
                    }
                    map.put("checkGroupNames",checkGroupNames.toString());
                    setmealNames.add(setmeal.getName());
                }
                map.put("setmealNames",setmealNames.toString());
            }
        }
        return mapList;
    }

}
